package com.example.kotlinfindcar.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log

import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.kotlinfindcar.R
import com.example.kotlinfindcar.databinding.ActivityMainBinding
import com.example.kt_all.ui.AllFragment
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.util.Constants
import com.example.kt_fast.ui.FastFragment
import com.example.kt_me.ui.MineFragment

@Route(path = Constants.PATH_MAIN)
class MainActivity : BaseActivity<ActivityMainBinding>() {
    private lateinit var fragmentList: MutableList<Fragment>
    var oldFragmentIndex: Int = 0
    var f1: Fragment? = null
    var f2: Fragment? = null
    var f3: Fragment? = null


    override fun getLayoutID(): Int {
        return R.layout.activity_main
    }

    override fun init() {
        mBind.bnv.setOnItemSelectedListener {
            when(it.itemId){
                R.id.f1->{
                    switchFragment(0)
                    mBind.fName = resources.getString(R.string.tab_fast)
                    return@setOnItemSelectedListener true
                }
                R.id.f2->{
                    switchFragment(1)
                    mBind.fName = resources.getString(R.string.tab_all)
                    return@setOnItemSelectedListener true
                }
                R.id.f3->{
                    switchFragment(2)
                    mBind.fName = resources.getString(R.string.tab_me)
                    return@setOnItemSelectedListener true
                }
            }
            false

        }
        initFragment()
        fragmentList = mutableListOf(f1!!,f2!!,f3!!)
        switchFragment(0)
        mBind.fName = resources.getString(R.string.tab_fast)
    }

    @SuppressLint("MissingSuperCall")
    override fun onSaveInstanceState(outState: Bundle) {
        //super.onSaveInstanceState(outState)
        //解决fragment重叠问题
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d(TAG, "onConfigurationChanged: ")
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        Log.d(TAG, "onNewIntent: $intent")
    }

    private fun switchFragment(position: Int) {
        val targetFragment = fragmentList[position]
        val oldFragment = fragmentList[oldFragmentIndex]
        oldFragmentIndex = position
        val ft = supportFragmentManager.beginTransaction()
        if (oldFragment.isAdded) {
            ft.hide(oldFragment)
        }
        if (!targetFragment.isAdded) {
            ft.add(R.id.f_content, targetFragment)
        }
        ft.show(targetFragment).commitAllowingStateLoss()
    }

    private fun initFragment() {
        if (f1 == null) {
            f1 = FastFragment()
        }
        if (f2 == null) {
            f2 = AllFragment()
        }
        if (f3 == null) {
            f3 = MineFragment()
        }

    }
}